package com.itheima.mybatisplusdemo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mybatisplusdemo.domain.User;
import com.itheima.mybatisplusdemo.mapper.UserDao;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;

@SpringBootTest
@Slf4j
public class MybatisPlusPractice1 {

    @Autowired
    private UserDao userDao;

    /**
     * 统计每个号码的数量
     */
    @Test
    public void testCountByAge() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper
                .select("count(*) as count", "tel")
                .groupBy("tel");
        List<Map<String, Object>> maps = userDao.selectMaps(queryWrapper);
        for (Map<String, Object> map : maps) {
            log.info(map.toString());
        }
    }

    /**
     * 降序排序
     */

    @Test
    public void testTelOrderByDesc() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper
                .select("count(*) as count", "age")
                .groupBy("age")
                //降序排序
                .orderByDesc("age");
        List<Map<String, Object>> maps = userDao.selectMaps(queryWrapper);
        for (Map<String, Object> map : maps) {
            log.info(map.toString());
        }
    }

    /**
     * 前5条
     */
    @Test
    public void testPage() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper
                .select("count(*) as count", "age")
                .groupBy("age")
                //降序排序
                .orderByDesc("age");
        IPage page = new Page(1, 5);
        userDao.selectMapsPage(page, queryWrapper);

        for (Object record : page.getRecords()) {
            log.info(record.toString());
            //log.info("{}", record);
        }
    }
}
